package com.example.module_marketers

import android.app.AlertDialog
import android.content.DialogInterface
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_common.view.BaseMvvmActivity
import com.example.module_marketers.adapter.MarketersAdapter
import com.example.module_marketers.databinding.ActivityResidencyApplicationMarketersBinding
import com.example.module_marketers.viewmodel.MarketersViewModel
/**
 * 智慧园区-04-商家推荐V1.1-张亦婷-20250512
 */
@Route(path = "/module_marketers/marketersActivity")
class ResidencyApplicationMarketersActivity : BaseMvvmActivity<ActivityResidencyApplicationMarketersBinding>() {

    @Autowired
    @JvmField
    var marketId:Int=0
    lateinit var marketersAdapter: MarketersAdapter
    var isClick:Boolean=true
    private val viewModel: MarketersViewModel by viewModels()

    override fun initViews() {
        super.initViews()
        ARouter.getInstance().inject(this)
        marketersAdapter= MarketersAdapter()
        binding.rvSp.adapter=marketersAdapter
        binding.rvSp.layoutManager= LinearLayoutManager(this)
        binding.btnWait.setTextColor(Color.parseColor("#FFFFFF"))
        binding.btnWait.setBackgroundColor(Color.parseColor("#10B981"))

        viewModel.getResidencyApplicationList()
        binding.back.setOnClickListener {
            finish()
        }
        binding.btnWait.setOnClickListener {
            isClick=true
            binding.btnWait.setTextColor(Color.parseColor("#FFFFFF"))
            binding.btnWait.setBackgroundColor(Color.parseColor("#10B981"))
            binding.btnConfirm.setTextColor(Color.parseColor("#6B7280"))
            binding.btnConfirm.setBackgroundColor(Color.parseColor("#FFFFFF"))
            viewModel.getResidencyApplicationList()
        }
        binding.btnConfirm.setOnClickListener {
            isClick=false
            binding.btnConfirm.setTextColor(Color.parseColor("#FFFFFF"))
            binding.btnConfirm.setBackgroundColor(Color.parseColor("#10B981"))
            binding.btnWait.setTextColor(Color.parseColor("#6B7280"))
            binding.btnWait.setBackgroundColor(Color.parseColor("#FFFFFF"))
            viewModel.getResidencyApplicationList()
        }
        marketersAdapter.setOnItemClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("是否通过审核")
                .setPositiveButton("确定",object : DialogInterface.OnClickListener{
                    override fun onClick(p0: DialogInterface?, p1: Int) {
                        viewModel.approveResidencyApplication(marketId)
                    }
                })
                .setNegativeButton("取消",null)
                .show()
        }
    }
    override fun observeDataChange() {
        viewModel.getResidencyApplicationListSuccess.observe(this){
            val data=if (isClick) it.filter { it.status=="新建" || it.status=="待审核" }
            else  it.filter { it.status=="已审核" || it.status=="驳回"}
            Log.d("marketerList","${data}")
            marketersAdapter.data.clear()
            marketersAdapter.data.addAll(data)
            marketersAdapter.notifyDataSetChanged()
        }
        viewModel.approveResidencyApplicationSuccess.observe(this){
            ToastUtils.showLong("已通过审核")
            viewModel.getResidencyApplicationList()
        }
    }

    override fun getLayoutId(): Int =R.layout.activity_residency_application_marketers
}